package swing;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.util.List;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import org.quartz.Scheduler;

import pojo.Schedule;

import dao.ScheduleDAO;


import main.SetupScheduler;
import main.ThreadPoolJob;

public class ScheduleGUI extends JPanel {
	private ScheduleDAO scheduledao = new ScheduleDAO();
	private SetupScheduler ss = new SetupScheduler();
	private List<Schedule> schedules;
	
	private JButton btnCollectButton;
	private JButton btnCancelButton;
	private JButton btnSaveButton;
	private JButton btnRevertButton;
	private JComboBox  howOftenList;
	private JComboBox whatDayList;
	private JComboBox whatTimeList;
	private JCheckBox cbUseSchedule;
	private String[] howOftenStrings = {"Daily", "Weekly", "Monthly"};
	private String[] whatDayStrings1 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
			   							"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "Last day"
			  				   		   };
	private String[] whatDayStrings2 = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	private String[] whatTimeStrings = {"12:00 AM (midnight)", "1:00 AM", "2:00 AM", "3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM",
									    "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 PM (noon)", "1:00 PM", "2:00 PM", "3:00 PM",
									    "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM",
									   };
	/**
	 * Create the panel.
	 */
	@SuppressWarnings("deprecation")
	public ScheduleGUI() {
		setLayout(null);
		
		btnCollectButton = new JButton("Collect books now");
		btnCollectButton.setBounds(1, 1, 150, 23);
		add(btnCollectButton);
		btnCollectButton.setEnabled(true);
		
		btnCancelButton = new JButton("Cancel");
		btnCancelButton.setBounds(240, 1, 150, 23);
		add(btnCancelButton);
		btnCancelButton.setEnabled(false);
		
		btnCollectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//btnCollectButton.setEnabled(false);
				//btnCancelButton.setEnabled(true);
				ss.startNow();
				
			}
		});
		btnCancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//btnCollectButton.setEnabled(true);
				//btnCancelButton.setEnabled(false);
				//ss.cancelNow();
			}
		});
		
		cbUseSchedule = new JCheckBox("Run collect books on a schedule (recommended)", true);
		cbUseSchedule.setBounds(1, 30, 300, 23);
		add(cbUseSchedule);
		
		cbUseSchedule.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				if(!cbUseSchedule.isSelected()){
					howOftenList.setEnabled(false);
					whatDayList.setEnabled(false);
					whatTimeList.setEnabled(false);
				}else{
					howOftenList.setEnabled(true);
					if(howOftenList.getSelectedIndex() == 0)
						whatDayList.setEnabled(false);
					else
						whatDayList.setEnabled(true);
					whatTimeList.setEnabled(true);
				}
			}
		});
		
		JLabel lblHowOften = new JLabel("How often:");
		lblHowOften.setBounds(1, 60, 60, 14);
		add(lblHowOften);
		
		JLabel lblWhatDay = new JLabel("What day:");
		lblWhatDay.setBounds(1, 100, 60, 14);
		add(lblWhatDay);
		
		JLabel lblWhatTime = new JLabel("What time:");
		lblWhatTime.setBounds(1, 140, 60, 14);
		add(lblWhatTime);
		
		howOftenList = new JComboBox(howOftenStrings);
		howOftenList.setBounds(100, 57, 200, 25);
		add(howOftenList);
		
		whatDayList = new JComboBox();
		whatDayList.setBounds(100, 97, 200, 25);
		whatDayList.setEnabled(false);
		add(whatDayList);
		
		whatTimeList = new JComboBox(whatTimeStrings);
		whatTimeList.setBounds(100, 134, 200, 25);
		add(whatTimeList);
		
		howOftenList.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(howOftenList.getSelectedIndex() == 0){
					whatDayList.removeAllItems();
					whatDayList.setEnabled(false);
				}
					
				else{
					if(!whatDayList.isEnabled())
						whatDayList.setEnabled(true);
					whatDayList.removeAllItems();
					if(howOftenList.getSelectedIndex() == 1){
						for(String item: whatDayStrings2){
							whatDayList.addItem(item);
						}
						
					}else{
						for(String item: whatDayStrings1){
							whatDayList.addItem(item);
						}
					}
				}
					
						
				
			}
		});
		btnSaveButton = new JButton("Save");
		btnSaveButton.setBounds(1, 200, 150, 23);
		add(btnSaveButton);
		
		btnRevertButton = new JButton("Revert");
		btnRevertButton.setBounds(240, 200, 150, 23);
		add(btnRevertButton);
		
		btnSaveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
 				Schedule s = scheduledao.insert(howOftenList.getSelectedIndex(), whatDayList.getSelectedIndex(), whatTimeList.getSelectedIndex(), cbUseSchedule.isSelected()?"false":"true");
 				ss.setupOrUpdate(s);
			}
		});
		btnRevertButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				schedules = scheduledao.getList();
				if(!schedules.isEmpty()){	
					//cbUseSchedule.setSelected(true);
					Schedule temp = schedules.get(schedules.size() - 1);
					howOftenList.setSelectedIndex(temp.getHowOftenIndex());
					whatDayList.setSelectedIndex(temp.getWhatDayIndex());
					whatTimeList.setSelectedIndex(temp.getWhatTimeIndex());
					if(temp.getManualCollect().compareTo("true") == 0){
						//cbUseSchedule.doClick();
						if(cbUseSchedule.isSelected())
							cbUseSchedule.doClick();
					}
					else{
						if(!cbUseSchedule.isSelected())
							cbUseSchedule.doClick();
					}	
				}
			}
		});
		//cbUseSchedule.setSelected(true);
		schedules = scheduledao.getList();
		if(schedules.isEmpty())
			cbUseSchedule.setSelected(true);
		else{
			Schedule temp = schedules.get(schedules.size() - 1);
			howOftenList.setSelectedIndex(temp.getHowOftenIndex());
			whatDayList.setSelectedIndex(temp.getWhatDayIndex());
			whatTimeList.setSelectedIndex(temp.getWhatTimeIndex());
			if(temp.getManualCollect().compareTo("true") == 0){
				cbUseSchedule.doClick();
			}
			else{
			}
			ss.setupOrUpdate(schedules.get(schedules.size() - 1));
		}
		//ObserveJobUpdateGUI r = new ObserveJobUpdateGUI();
		//Thread t = new Thread(r);
		//t.start();
	}
	/*public class ObserveJobUpdateGUI implements Runnable{
		private boolean updateGuiSth1 = false;
		private boolean updateGuiSth2 = false;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			while(true){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(ThreadPoolJob.getIsJobRunning1()){
					
					if(!updateGuiSth1){
						if(btnCollectButton.isEnabled())
							btnCollectButton.setEnabled(false);
						if(!btnCancelButton.isEnabled())
							btnCancelButton.setEnabled(true);
						if(btnSaveButton.isEnabled())
							btnSaveButton.setEnabled(false);
						if(btnRevertButton.isEnabled())
							btnRevertButton.setEnabled(false);
						if(howOftenList.isEnabled())
							howOftenList.setEnabled(false);
						if(whatDayList.isEnabled())
							whatDayList.setEnabled(false);
						if(whatTimeList.isEnabled())
							whatTimeList.setEnabled(false);
						if(cbUseSchedule.isEnabled())
							cbUseSchedule.setEnabled(false);
						updateGuiSth1 = true;
						updateGuiSth2 = false;
					}
				}else{
					//System.out.println("schedule");
					if(!updateGuiSth2){
						if(!btnCollectButton.isEnabled())
							btnCollectButton.setEnabled(true);
						if(btnCancelButton.isEnabled())
							btnCancelButton.setEnabled(false);
						if(!btnSaveButton.isEnabled())
							btnSaveButton.setEnabled(true);
						if(!btnRevertButton.isEnabled())
							btnRevertButton.setEnabled(true);
						if(!howOftenList.isEnabled())
							howOftenList.setEnabled(true);
						if(!whatDayList.isEnabled())
							whatDayList.setEnabled(true);
						if(!whatTimeList.isEnabled())
							whatTimeList.setEnabled(true);
						if(!cbUseSchedule.isEnabled())
							cbUseSchedule.setEnabled(true);
						updateGuiSth1 = false;
						updateGuiSth2 = true;
					}
				}	
			}
		}	
	}*/
}
